﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Entity;
using BUS;

public partial class Admin_QuanLyKhachHang : System.Web.UI.Page
{
    DataTable dtKH;
    DataTable dtCapDo;
    DataTable dt;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Username"] == null)
            Response.Redirect("../Default.aspx");
        if (Boolean.Parse(Session["QuanLy"].ToString()) == false)
            Response.Redirect("../Default.aspx");
        if (!IsPostBack)
        {            
            bindgrid();
            cmbCapDo.DataSource = dtCapDo = QuanLyKhachHangBUS.LayCapDo();
            cmbCapDo.DataTextField = "CapDo";
            cmbCapDo.DataBind();
        }
    }
    private void gotoAnchorScript(string anchor)
    {
        ClientScript.RegisterStartupScript(this.GetType(), "hash", "location.hash = '#" + anchor + "';", true);
    }
    protected void bindgrid()
    {
        gvKhachHang.DataSource = dtKH = QuanLyKhachHangBUS.LayThongTinKhachHang();
        gvKhachHang.DataBind();
    }
    protected void gvKhachHang_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvKhachHang.PageIndex = e.NewPageIndex;
        bindgrid();
        gotoAnchorScript("list");
    }

    protected void gvKhachHang_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        String cmnd = gvKhachHang.Rows[e.RowIndex].Cells[1].Text;
        QuanLyKhachHangBUS.XoaKhachHang(cmnd);
        bindgrid();
        gotoAnchorScript("list");
    }

    protected void gvKhachHang_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        Int32 idx = Int32.Parse(e.CommandArgument.ToString());
        if (e.CommandName.Equals("Sua"))
        {
            btnThemKH.Visible = false;
            tblThemKH.Visible = true;
            txtCMND.ReadOnly = true;
            txtTen.Text = HttpUtility.HtmlDecode(gvKhachHang.Rows[idx].Cells[0].Text);
            txtCMND.Text = HttpUtility.HtmlDecode(gvKhachHang.Rows[idx].Cells[1].Text);
            txtDiaChi.Text = HttpUtility.HtmlDecode(gvKhachHang.Rows[idx].Cells[2].Text);
            txtDienThoai.Text = gvKhachHang.Rows[idx].Cells[3].Text;
            txtEmail.Text = HttpUtility.HtmlDecode(gvKhachHang.Rows[idx].Cells[4].Text);
            cmbCapDo.Text = HttpUtility.HtmlDecode(gvKhachHang.Rows[idx].Cells[5].Text);
            lbNhap.Text = "Cập nhật thông tin khách hàng";
            gotoAnchorScript("form");
        }
    }
    protected void btnThemKH_Click(object sender, EventArgs e)
    {
        tblSuaKH.Visible = false;
        tblThemKH.Visible = true; 
        gotoAnchorScript("form");
    }
    protected void btnCapNhat_Click(object sender, EventArgs e)
    {
        if (btnCapNhat.Text == "Cập nhật")
        {
            KhachHangThanThiet kh = new KhachHangThanThiet();
            kh.cmnd = txtCMND.Text.Trim();
            kh.tenkh = txtTen.Text.Trim();
            kh.diachi = txtDiaChi.Text.Trim();
            kh.sodienthoai = txtDienThoai.Text.Trim();
            kh.email = txtEmail.Text.Trim();
            kh.maloaikhachhang = QuanLyKhachHangBUS.LayMaLoaiKH(cmbCapDo.Text);
            QuanLyKhachHangBUS.CapNhatKhachHang(kh);
            Response.Redirect("QuanLyKhachHang.aspx#list");
        }
        if (btnCapNhat.Text == "Lưu")
        {
            KhachHangThanThiet kh = new KhachHangThanThiet();
            kh.cmnd = txtCMND.Text.Trim();
            kh.tenkh = txtTen.Text.Trim();
            kh.diachi = txtDiaChi.Text.Trim();
            kh.sodienthoai = txtDienThoai.Text.Trim();
            kh.email = txtEmail.Text.Trim();
            kh.ngaydangky = DateTime.Now;
            kh.maloaikhachhang = QuanLyKhachHangBUS.LayMaLoaiKH(cmbCapDo.Text);
            QuanLyKhachHangBUS.ThemKhachHang(kh);
            Response.Redirect("QuanLyKhachHang.aspx#list");
        }
    }
    protected void btnHuy_Click(object sender, EventArgs e)
    {
        Response.Redirect("QuanLyKhachHang.aspx#list");
    }

    protected void btnOK_Click(object sender, EventArgs e)
    {
        int mahd = int.Parse(txtMaHD.Text.Trim());
        dt = QuanLyKhachHangBUS.KiemTraHoaDon(mahd);
        if (dt.Rows.Count > 0)
        {
            tblSuaKH.Visible = true;
            tblThemKH.Visible = false;
            txtTen.Text = dt.Rows[0][0].ToString();
            txtTen.ReadOnly = true;
            txtDiaChi.Text = dt.Rows[0][1].ToString();
            txtDiaChi.ReadOnly = true;
            txtDienThoai.Text = dt.Rows[0][2].ToString();
            txtDienThoai.ReadOnly = true;
            if (float.Parse(dt.Rows[0][3].ToString()) >= 200000000)
            {
                cmbCapDo.Text = QuanLyKhachHangBUS.LayCapDo().Rows[1][0].ToString();
            }
            else
            {
                cmbCapDo.Text = QuanLyKhachHangBUS.LayCapDo().Rows[0][0].ToString();
            }
            btnCapNhat.Text = "Lưu";
            txtCMND.ReadOnly = false;
            lbNhap.Text = "Thêm khách hàng thân thiết";
            btnThemKH.Visible = false;
            gotoAnchorScript("form");
        }
    }
}
